﻿using System;
using System.IO.Compression;
using System.Web;
using GONES.Tools.Common;

namespace GONES.Tools.Web.HttpModules
{
    public class PermissionModule : IHttpModule
    {
        public void Init(HttpApplication context)
        {
            context.BeginRequest += new EventHandler(context_BeginRequest);
            context.AcquireRequestState += new EventHandler(context_AcquireRequestState);
            context.EndRequest += new EventHandler(context_EndRequest);
        }


        private DateTime _requestStartTime;

        private void context_BeginRequest(object sender, EventArgs e)
        {
            _requestStartTime = DateTime.Now;
        }

        private void context_AcquireRequestState(object sender, EventArgs e)
        {
            HttpContext context = ((HttpApplication) sender).Context;
            //动态页面
            if (context.Request.Path.ToUpper().EndsWith(".ASPX"))
            {
                IUserInfo userInfo = new CWebUserInfo();
                if (userInfo.CLoginInfo.UserId == "")
                    return;

                //UserCacheManager<LoginInfo>.Add(userInfo.CLoginInfo.UserId, userInfo.CLoginInfo);
            }
        }

        private void context_EndRequest(object sender, EventArgs e)
        {
            HttpContext context = ((HttpApplication) sender).Context;

            //动态页面
            if (context.Request.Path.ToUpper().EndsWith(".ASPX") && !context.Request.Path.ToLower().Contains("cxt.aspx") && !context.Request.Path.ToLower().Contains("upload.aspx") && string.IsNullOrEmpty(context.Request.QueryString["ajax"]))
                context.Response.Write(string.Format("<!--页面执行了 {0} 毫秒-->",
                                                     (DateTime.Now.Subtract(_requestStartTime).TotalMilliseconds)));
        }

        public void Dispose()
        {
            
        }
    }
}